<?php

/**
 * 特采需求模型
 * QiuXiangCheng
 * 2016/07/28
 */
defined('APPLICATION_PATH') or die;
class Purchase_Model_SpecialDemand extends Model_Base {

	private $specialModel;

	/**
	 * 注意这里没有9 因为字段中没有设置9
	 * @var [type]
	 */
	public static $fieldsFromExcel = [
		0 => 'goods_sn',
		1 => 'apply_num',
		2 => 'remark',
		3 => 'warehouse_code',
		4 => 'latest_time',
		5 => 'expect_from',
		6 => 'expect_to',
		7 => 'expect_sale_num',
		8 => 'stocking_type',
		9 => 'sale_user',
		10 => 'platform_code',
	];

	public static $examine_level = [
		1 => '待小组长审核',
		2 => '待大组长审核',
		3 => '待主管审核',
		4 => '待经理审核',
		5 => '审核通过',
		6 => '小组长驳回',
		7 => '大组长驳回',
		8 => '主管驳回',
		9 => '经理驳回',
	];

	public static $purchase_status = [
		0 => '-',
		1 => '已采购',
		2 => '待采购',
		3 => '部份采购',
		4 => '已失效',
	];

	/**
	 * 最外围的数组键代表仓库是否需要审核的ID
	 * 二维中代表该状态下生成特采之后各字段的状态
	 */
	const FETCH_EXAMINE_LEVEL = [
		1 => [
			'examine_status' => 7,
			'purchase_status' => 2,
			'examine_level' => 1,
			'billType' => 1,
		],
		2 => [
			'examine_status' => 1,
			'purchase_status' => 0,
			'examine_level' => 2,
			'billType' => 0,
		],
		3 => [
			'examine_status' => 1,
			'purchase_status' => 0,
			'examine_level' => 3,
			'billType' => 0,
		],
		4 => [
			'examine_status' => 1,
			'purchase_status' => 0,
			'examine_level' => 4,
			'billType' => 0,
		],
	];

	public function __construct() {

		$this->specialModel = new Purchase_Model_ProductCheck;
	}

	/**
	 * 从特采需求中取得相关的特采需求
	 * @param 仓库编码（数组）
	 * @return Array
	 */
	public function getSpecialsByWarehouseCode($where = '', $order = ' ORDER BY create_time DESC', $limit = '0, 15') {

		if (empty($where)) {
			$where = 'WHERE billType = 1';
		} else {
			$where .= ' AND billType = 1';
		}
		$group = ' GROUP BY spd.goods_sn, spd.warehouse_code ';
		$sql = '
				SELECT *, SUM(tallying_num) sum_tallying_num, SUM(apply_num) sum_apply_num, SUM(return_num) sum_return_num,
				SUM(expect_sale_num) sum_expect_sale_num, SUM(purchase_num) sum_purchase_num
				FROM p_special_purchase spd'; //  LEFT JOIN p_special_purchase_demand_examine spde ON spd.id = spde.psd_id
		return self::db()->fetchAll($sql . $where . $group . $order);
	}

	/**
	 * 通过SKU+仓库取回货数量
	 * @return interger
	 */
	public static function fetchReturnAmount($sid) {
		$arr = explode(',', $sid);
		$where = self::mysql()->quoteInto('sid in (?)', $arr);
		return self::mysql()->fetchAll('SELECT return_amount, return_date FROM p_special_return_log WHERE ' . $where);
	}

	/**
	 * 通过
	 * @return [type] [description]
	 */
	public static function fetchRemarkLogList($where) {

		return self::mysql()->fetchAll('SELECT * FROM p_special_purchase sp ' . $where);
	}

	/**
	 * 生成特采需求WHERE条件
	 * @param get参数
	 * @param $where
	 * @return string
	 */
	public static function fetchFilterDemand($get, $where) {

		empty($where) ? $where = ' WHERE ' : $where .= ' AND ';
		$where .= 'sp.billType = 1 AND (sp.purchase_status = 2 OR sp.purchase_status = 3)';
		if (isset($get['examine_status']) && $get['examine_status'] != '-') {
			$where .= 'AND bs.examine_status = ' . $get['examine_status'];
		}
		return $where;
	}

	/**
	 * 通过仓库编码取得该仓库的特采数据/特采需求及需求的审核等级
	 * @param 仓库编码
	 * @param 要取得的数据 [special_data_examine|special_demand_examine|demand_examine]
	 * @return interger
	 */
	public static function confirmForChoiceExamineLevel($warehouse_code, $f = 'special_data_examine') {

		return self::mysql()->fetchOne('SELECT ' . $f . ' examine_level FROM c_warehouse WHERE warehouse_code = "' . $warehouse_code . '"');
	}

	/**
	 * 加入EXCEL到数据库
	 * @param EXCEL数据
	 * @return interval
	 */
	public function loadExcel($data) {

		// 通过所属仓库设定的审核规则来确定各字段状态
		// 			foreach($data as $k => $values){
		// 				$level_examine = Purchase_Model_SpecialDemand::confirmForChoiceExamineLevel($values['warehouse_code']);
		// 				$data[$k]['examine_status'] = self::FETCH_EXAMINE_LEVEL[$level_examine]['examine_status'];
		// 				$data[$k]['purchase_status'] = self::FETCH_EXAMINE_LEVEL[$level_examine]['purchase_status'];
		// 				$data[$k]['billType'] = self::FETCH_EXAMINE_LEVEL[$level_examine]['billType'];
		// 				$data[$k]['examine_level'] = $level_examine;
		// 			}
		return $this->insertBatch($this->db(), 'p_special_purchase', $data);
	}

	/**
	 * 根据仓库类型取得相关数据
	 * 1-自主仓 2-合约仓 3-公共仓 4-虚拟仓 5-平台虚拟仓
	 * @return array();
	 */
	public function getWarehouseOfPlatform($type = 5, $fields = '*') {

		return $this->db()->fetchAll('SELECT ' . $fields . ' FROM c_warehouse WHERE type = "' . $type . '"');
	}

	/**
	 * 取得需求的数量
	 * @return interger
	 */
	/*public static function countSepcaialDemandPurchase(){

			return self::mysql() -> fetchAssoc('SELECT COUNT(*) FROM p_special_purchase WHERE ');
		}*/
}